<?php

namespace App\Trip\Orders\BookOrders\Plane;

use App\Trip\Enums\Plane\PlaneProviders;
use App\Trip\Enums\TripTypes;
use App\Trip\Orders\TransferFlag;
use App\Trip\Plane\Interfaces\Result\Abstracts\AbstractFlightItem;
use App\Trip\Plane\PlaneInfo\FlightInfo;
use Exception;

class BuildOrderData
{
    private string             $bookingTagKey;
    private AbstractFlightItem $seatItem;

    /**
     * @throws Exception
     */
    public function __construct(PlaneProviders $provider, string $bookingTagKey)
    {
        $this->bookingTagKey = $bookingTagKey;
        $service             = $provider->getPlaneQueryStrategy();
        $this->seatItem      = $service->parseFlightItem($this->bookingTagKey);
    }

    /**
     * @throws Exception
     */
    public function getFlightInfo(): FlightInfo
    {
        // 已选定的
        $flight_info = $this->seatItem->getFlight()->getFlightInfo();
        $flight_info->setProviderPrices($this->seatItem->getProviderPrices());
        $flight_info->setCheckPrices($this->seatItem->getCheckPrices());
        $flight_info->setProviderFuelFees($this->seatItem->getProviderFuelFees());
        $flight_info->setProviderConstructionFees($this->seatItem->getProviderConstructionFees());
        $flight_info->setCabinName($this->seatItem->getCabinName());
        $flight_info->setCabin($this->seatItem->getCabin());
        $flight_info->setCabins($this->seatItem->getCabins());
        $flight_info->setProviderPolicyId($this->seatItem->getProviderExtensionId());
        return $flight_info;
    }

    public function getOrderData(): array
    {
        $flight = $this->seatItem->getFlight();
        return [
            'provider'           => '',
            'flight_no'          => $flight->getFlightNo(),
            'from_code'          => $flight->getDepartCode(),
            'to_code'            => $flight->getArriveCode(),
            'from_name'          => $flight->getDepartName(),
            'to_name'            => $flight->getArriveName(),
            'short_from_name'    => $flight->getDepartName(true),
            'short_to_name'      => $flight->getArriveName(true),
            'from_tower'         => $flight->getFromTower(),
            'to_tower'           => $flight->getToTower(),
            'cabin'              => $flight->getCabin(),
            'from_name_uy'       => $flight->getDepartNameUy(),
            'to_name_uy'         => $flight->getArriveNameUy(),
            'short_from_name_uy' => $flight->getDepartNameUy(true),
            'short_to_name_uy'   => $flight->getArriveNameUy(true),
            'start_at'           => $flight->getDepartAt(),
            'arrive_at'          => $flight->getArriveAt(),
            'trip_no'            => 1,
            'trip_type'          => TripTypes::PLANE->trip_type(),
            'transfer_flag'      => TransferFlag::ORDER_TRANSFER_FLAG_NONE,
        ];
    }
}